package com.rowenci.iot_agriculture.config;

import com.rowenci.iot_agriculture.interceptor.AdminInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import java.util.ArrayList;
import java.util.List;

@Configuration
public class RegisterInterceptor extends WebMvcConfigurationSupport {
    @Override
    protected void addInterceptors(InterceptorRegistry registry) {
        List<String> interceptorList = new ArrayList<>();

        List<String> uninterceptorList = new ArrayList<>();
        uninterceptorList.add("/");
        uninterceptorList.add("/login/**");
        uninterceptorList.add("/admin/**");
        uninterceptorList.add("/static/**");
        uninterceptorList.add("/greenhouse/**");
        uninterceptorList.add("/access/**");
        uninterceptorList.add("/account/**");
        uninterceptorList.add("/com/**");

        registry.addInterceptor(new AdminInterceptor()).addPathPatterns(interceptorList).excludePathPatterns(uninterceptorList);
        super.addInterceptors(registry);
    }

    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
        super.addResourceHandlers(registry);
    }

    @Override
    protected void addViewControllers(ViewControllerRegistry registry) {
        /**
         * 访问根地址时，跳转到login页面
         */
        registry.addViewController("/").setViewName("login");
        /**
         * 被拦截时，跳转到login页面
         */
        super.addViewControllers(registry);
    }


}
